package cn.jobstart.controller;

import cn.jobstart.common.NumberUtil;
import cn.jobstart.common.SkMap;
import cn.jobstart.common.StringUtil;
import cn.jobstart.dto.JobDTO;
import cn.jobstart.dto.JobPageDTO;
import cn.jobstart.enums.JobNextFlagEnum;
import cn.jobstart.export.JobDomainExport;
import cn.jobstart.model.Job;
import cn.jobstart.modelExt.JobExt;
import cn.jobstart.pubInter.IBaseService;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.JobSearchExt;
import cn.jobstart.service.IJobService;
import cn.jobstart.vo.JobDetailDomainVO;
import cn.jobstart.vo.JobDetailVO;
import cn.jobstart.vo.JobDomainVO;
import cn.jobstart.vo.JobListVO;
import cn.jobstart.web.common.SkJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author sunke
 * @Date 2023-09-15 14:55:16
 * @description JobController      职位名称
 */
@Api(tags = "后台管理系统-职位名称相关", description = "职位名称相关接口")
@RestController(value = "jobRestfulController")
@RequestMapping("/job/")
@Validated
public class JobController extends ApiManageController<JobDTO, JobPageDTO, JobExt, JobSearchExt, JobDomainVO, JobDetailDomainVO, JobDetailVO, JobListVO, String> {

    @Resource(name = "jobService")
    private IJobService jobService;


    @Override
    public boolean beforePostAdd(JobDTO dto, JobExt domain, HttpServletRequest request, HttpSession session) throws SkException {
        if (StringUtil.isNullOrEmpty(dto.getUpId())) {
            domain.setLevel(1);
        } else {
            JobExt upJob = jobService.getDetail(dto.getUpId());
            domain.setLevel(NumberUtil.parseInt(upJob.getLevel()) + 1);
        }

        return super.beforePostAdd(dto, domain, request, session);

    }


    @Override
    public void afterPostAdd(JobDTO dto, JobExt domain, JobDetailVO jobDetailVO, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostAdd(dto, domain, jobDetailVO, request, session);
        jobService.updateNextFlagByJobId(domain.getUpId(), JobNextFlagEnum.Y.getType());
    }

    @Override
    public void afterPostMod(JobDTO dto, JobExt domain, SkJsonResult<JobDetailVO> jsonResult, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostMod(dto, domain, jsonResult, request, session);
        //说明上级没变，等级就没变
        if (StringUtil.toString(domain.getUpId()).equalsIgnoreCase(StringUtil.toString(dto.getUpId()))) {
            return;
        }
        int level = 1;
        if ("".equalsIgnoreCase(domain.getUpId())) {
            level = 1;
        } else {
            JobExt upJob = jobService.getDetail(domain.getUpId());
            level = NumberUtil.parseInt(upJob.getLevel(), 1) + 1;
            jobService.updateAllSubLevelByJobId(domain.getId(), level, true);
            jobService.updateNextFlagByJobId(domain.getUpId(), JobNextFlagEnum.Y.getType());
        }
        jobService.updateNextFlagByJobId(dto.getUpId(), -1);


    }


    @ApiOperation(value = "顶级列表", notes = "顶级列表")
    @PostMapping(value = "topList")
    public SkJsonResult<List<JobDetailDomainVO>> topList(@RequestBody JobPageDTO pageDTO, HttpServletRequest request, HttpSession session) throws SkException {


        JobSearchExt search = map(pageDTO, JobSearchExt.class);
        search.setTopFlag(1);


        return SkJsonResult.ok().setData(mapList(jobService.getAllList(search), JobDetailDomainVO.class));
    }


    @Override
    public void afterPostLogicDelete(String id, HttpServletRequest request, HttpSession session) throws SkException {

        super.afterPostLogicDelete(id, request, session);

        JobExt job = jobService.getDetail(id);

        jobService.updateDataBySearch(SkMap.getInstance().addObjToMap(Job.UP_ID, job.getUpId()), JobSearchExt.getInstance().setUpIdEq(id));

        jobService.updateNextFlagByJobId(job.getUpId(), -1);

        jobService.updateAllSubLevelByJobId(job.getUpId(), NumberUtil.parseInt(job.getLevel()) - 1, false);


    }

    @ApiOperation(value = "权限下级树", notes = "权限下级树", httpMethod = "POST")
    @PostMapping({"tree/{level}"})
    public SkJsonResult<List<JobDetailDomainVO>> tree(@PathVariable int level, HttpServletRequest request, HttpSession session) throws SkException {

        return SkJsonResult.ok().setData(jobService.tree(level));

    }

    @Override
    public IBaseService<JobExt, JobSearchExt, String> getBaseService() throws SkException {
        return jobService;
    }

    @Override
    public String getBaseRoute() {
        return "/job/";
    }

    /**
     * 当前模板名称
     *
     * @return
     */
    @Override
    public String getModelName() {
        return "职位名称";
    }

    @Override
    public Class exportExcelClass() {
        return JobDomainExport.class;
    }

    @Override
    public Set<String> totalMapSet() {
        Set totalMapSet = new HashSet();
        totalMapSet.add("count(job.j_id) as countNum");
        return totalMapSet;
    }


}